package cn.gson.fengboot.model.service;

import cn.gson.fengboot.common.CommonFunction;
import cn.gson.fengboot.model.DmypojisVo.Myrolesone;
import cn.gson.fengboot.model.DmypojisVo.Myrolethree;
import cn.gson.fengboot.model.dao.*;
import cn.gson.fengboot.model.mapper.DempMapper;
import cn.gson.fengboot.model.mapper.DmenufromMapper;
import cn.gson.fengboot.model.mapper.DrolesMapper;
import cn.gson.fengboot.model.pojos.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class DrolesService {


    @Autowired
    DrolesMapper mapper;

    @Autowired
    DroleDao droleDao;

    @Autowired
    DmenuschiredDao dmenuschiredDao;

    @Autowired
    DrolesmenuDao drolesmenuDao;

    @Autowired
    DlistroleDao dlistroleDao;

    @Autowired
    DrolesMapper drolesMapper;

    @Autowired
    DempMapper dempMapper;

    @Autowired
    DdepDao ddepDao;

    @Autowired
    DdepandmenusDao ddepandmenusDao;

    @Autowired
    DempDao dempDao;

    @Autowired
    DmenufromMapper dmenufromMapper;

    public List<Myrolesone> myroles(Integer roleId) {
        /*System.out.println("我是岗位id"+roleId);
        QroleFrom qroleFrom = droleDao.findById(roleId).get();
        Integer depId = qroleFrom.getDepId().getDepId();//找到该角色的部门id
        Integer formenus = formenus(depId);
        System.out.println("返回值id："+formenus);
        if(formenus!=-1){//
            return mapper.myroles(formenus);//返回部门权限组
        }*/
        QroleFrom qroleFrom = droleDao.findById(roleId).get();
        Integer depId = qroleFrom.getDepId().getDepId();//找到该角色的部门id
        List<Ddepandmenus> allByDepId = ddepandmenusDao.findAllByDepId(depId);//判断该部门是否有权限
        if(allByDepId.size()>0){
            return mapper.myroles(depId);//返回部门权限组
        }
        /*Integer formenus = formenus(depId);
        System.out.println("返回值id："+formenus);
        if(formenus!=-1){//
        }*/

        return null;
    }

    public Integer formenus(Integer depId){
        Integer id = depId;
        for(int i = -1 ;i<0;i--){
            List<Ddepandmenus> allByDepId = ddepandmenusDao.findAllByDepId(id);//判断该部门是否有权限
            if(allByDepId.size()<=0){//如果为空的话，就查询父部门的权限
                QdepFrom byDepId = ddepDao.findByDepId(id);
                if(byDepId.getUpDep().getDepId()!=null){
                    id = byDepId.getUpDep().getDepId();
                }else {
                    id = -1;
                }
            }else {//返回有菜单权限的上级部门
                break;
            }
        }
        return id;
    }

    public void saverolesarr(String rolesarr) {
        JSONObject obj = (JSONObject) JSONObject.parse(rolesarr);
        Integer roleId = obj.getObject("roleId", Integer.class);//取出角色ID
        drolesmenuDao.deleteByRoleId(roleId);//先清空该角色所有权限
        List mycheckedarr = obj.getObject("mycheckedarr", List.class);//取出权限组

        JSONArray json = JSONArray.parseArray(String.valueOf(mycheckedarr));
        JSONObject jsonObject = null;

        List a = new ArrayList();
        Qrolesmenu qrolesmenu =null;

        for (int i = 0; i < json.size(); i++) {
            ObjectMapper objectMapper = new ObjectMapper();
            qrolesmenu = new Qrolesmenu();
            jsonObject = (JSONObject) json.get(i);//获取每一行转成JSON对象
            Integer tid = jsonObject.getInteger("tid");
            Integer wid = null;
            QmenuFrom selectwid = null;

            Object al = jsonObject.getJSONObject("liro");
            Dlistrole dlistrole = objectMapper.convertValue(al, Dlistrole.class);//判断对象是否为空
            System.out.println("我是列表权限");
            System.out.println(dlistrole);
            qrolesmenu.setLiro(dlistrole);
            qrolesmenu.setRoleId(roleId);

            if(tid==null){//如果第三层菜单为空就查询第二层
                wid = jsonObject.getInteger("wid");
                selectwid = dmenufromMapper.selectbutid(wid);//查询该(第二层)菜单ID对象
                System.out.println("我是上级对象"+selectwid.toString());
                qrolesmenu.setMeId(wid);
            }else{
                selectwid = dmenufromMapper.selectbutid(tid);//查询该（第三层）菜单ID对象
                qrolesmenu.setMeId(tid);
            }
            drolesmenuDao.save(qrolesmenu);
        }
    }

    public List<Myrolethree> selectmyallroles(Integer roleId) {
        return drolesMapper.myallroles(roleId);
    }

    public List<Dlistrole> selectlistroles() {
        return (List) dlistroleDao.findAll();
    }


    public void savemynewroles(QroleFrom qroleFrom) {
        droleDao.save(qroleFrom);
    }

    public Integer myisnomyroles(Integer roleId) {
        List<Integer> myisnomyroles = drolesMapper.myisnomyroles(roleId);
        if(myisnomyroles.size()>0){
            return 1;
        }else {
            droleDao.deleteById(roleId);
            return 0;
        }
    }

    public List<QroleFrom> findAllByDepId(Integer depId){
        List<QroleFrom> allByDepId = drolesMapper.findAllByDepId(depId);
        System.out.println("我送："+allByDepId.size());
        Iterator<QroleFrom> iterator = allByDepId.iterator();
        while (iterator.hasNext()){
            QroleFrom next = iterator.next();
            String roleName = next.getRoleName();
            if(roleName.indexOf("经理")>0){//如果有经理的职位
                QempFrom byRoleId = dempMapper.findByRoleId(next.getRoleId());//查询该职位是否有员工
                System.out.println(CommonFunction.objCheckIsNull(byRoleId));
                if(!CommonFunction.objCheckIsNull(byRoleId)){//如果不为空就返回的集合去掉该职位
                    iterator.remove();//调用iterator.remove()方法。避免抛出异常ConcurrentModificationException
                }
            }
        }
        return allByDepId;
    }

}
